package controller

import (
	"net/http"
	"strconv"

	"gitee.com/ysl729895/go-mall/internal/model"
	"gitee.com/ysl729895/go-mall/internal/service"
	"github.com/gin-gonic/gin"
)

// RolePermissionController 角色-权限分配控制器，处理角色分配/移除权限、查询角色权限等 HTTP 请求

type RolePermissionController struct {
	service service.RolePermissionService
}

// NewRolePermissionController 构造函数，注入 RolePermissionService
func NewRolePermissionController(service service.RolePermissionService) *RolePermissionController {
	return &RolePermissionController{service: service}
}

// AssignPermissionToRole 给角色分配权限
// @Summary 给角色分配权限
// @Tags 角色权限分配
// @Accept json
// @Produce json
// @Param role_id path int true "角色ID"
// @Param permission_id body model.RolePermission true "权限ID"
// @Success 200 {string} string "分配成功"
// @Failure 400 {object} model.ErrorResponse
// @Failure 500 {object} model.ErrorResponse
// @Router /api/roles/{role_id}/permissions [post]
func (c *RolePermissionController) AssignPermissionToRole(ctx *gin.Context) {
	roleIDStr := ctx.Param("role_id")
	roleID, err := strconv.Atoi(roleIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的角色ID",
			Error:   err.Error(),
		})
		return
	}
	var req struct {
		PermissionID uint `json:"permission_id" binding:"required"`
	}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "请求参数格式不正确",
			Error:   err.Error(),
		})
		return
	}
	if err := c.service.AssignPermissionToRole(uint(roleID), req.PermissionID); err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "分配权限失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", gin.H{"role_id": roleID, "permission_id": req.PermissionID})
	ctx.Set("responseMessage", "分配权限成功")
}

// RemovePermissionsFromRole 移除角色所有权限
// @Summary 移除角色所有权限
// @Tags 角色权限分配
// @Param role_id path int true "角色ID"
// @Success 200 {string} string "移除成功"
// @Router /api/roles/{role_id}/permissions [delete]
func (c *RolePermissionController) RemovePermissionsFromRole(ctx *gin.Context) {
	roleIDStr := ctx.Param("role_id")
	roleID, err := strconv.Atoi(roleIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的角色ID",
			Error:   err.Error(),
		})
		return
	}
	if err := c.service.RemovePermissionsFromRole(uint(roleID)); err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "移除权限失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", gin.H{"role_id": roleID})
	ctx.Set("responseMessage", "移除权限成功")
}

// GetPermissionsByRoleID 查询角色所有权限
// @Summary 查询角色所有权限
// @Tags 角色权限分配
// @Param role_id path int true "角色ID"
// @Success 200 {array} model.Permission
// @Router /api/roles/{role_id}/permissions [get]
func (c *RolePermissionController) GetPermissionsByRoleID(ctx *gin.Context) {
	roleIDStr := ctx.Param("role_id")
	roleID, err := strconv.Atoi(roleIDStr)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, model.ErrorResponse{
			Code:    http.StatusBadRequest,
			Message: "无效的角色ID",
			Error:   err.Error(),
		})
		return
	}
	permissions, err := c.service.GetPermissionsByRoleID(uint(roleID))
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.ErrorResponse{
			Code:    http.StatusInternalServerError,
			Message: "查询角色权限失败",
			Error:   err.Error(),
		})
		return
	}
	ctx.Set("responseData", permissions)
}
